<template>
  <div class="tracetask-container">
    <v-layout wrap justify-center align-center>
      <v-flex md12 d-flex>
        <v-flex md2 px-2>
          <v-text-field v-model="queryForm.nickName" label="用户名称"></v-text-field>
        </v-flex>
        <v-flex md2 px-2>
          <v-select label="用户类型" :items="userType" v-model="queryForm.userType"></v-select>
        </v-flex>
        <v-flex md1 pl-2>
          <v-btn round class="subTitle" color="primary" @click="onQuery">
            <span>查询</span>
            <v-icon>search</v-icon>
          </v-btn>
        </v-flex>
        <v-flex md1>
          <v-btn round class="subTitle" color="blue darken-1" dark @click="onAddUser">
            <span>新增用户</span>
            <v-icon>add</v-icon>
          </v-btn>
        </v-flex>
      </v-flex>

      <!-- table -->
      <v-flex md12 class="elevation-1">
        <v-data-table :headers="headers" :loading="loading" :items="tableData" hide-actions>
          <template v-slot:items="props">
            <td class="text-xs-center">{{ props.item.userId }}</td>
            <td class="text-xs-center">{{ props.item.nickName }}</td>
            <td class="text-xs-center">{{ props.item.group === null ? '' : props.item.group.groupName }}</td>
            <td class="text-xs-center">{{ getTime(props.item.createTime) }}</td>
            <td class="text-xs-center">{{ getTime(props.item.modifiedTime) }}</td>
            <td class="text-xs-center mini-button">
              <v-btn
                round
                small
                outline
                class="subTitle"
                color="indigo lighten-2"
                @click="onModify(props.item)"
              >
                <div class="mr-1">编辑</div>
              </v-btn>

              <v-btn
                round
                small
                outline
                class="subTitle"
                color="red lighten-2"
                @click="onDelete(props.item)"
              >
                <div class="mr-1">删除</div>
              </v-btn>
            </td>
          </template>
          <template v-slot:no-data>
            <v-alert :value="true" outline color="warning" icon="info">暂无数据</v-alert>
          </template>
        </v-data-table>

        <v-layout class="pagination-wrapper">
          <v-flex md10 d-flex justify-start align-center>
            <v-flex d-flex md2 justify-start align-center>
              <v-flex md4 class="title-color">显示</v-flex>
              <v-flex md4>
                <v-select @change="onPageOptionChange" :items="pageOptions" v-model="pagination.limit"></v-select>
              </v-flex>
              <v-flex md4 class="title-color">条/页</v-flex>
            </v-flex>
            <v-flex md2 class="title-color">共 {{ pagination.pageCount }} 条数据</v-flex>
            <v-flex md2></v-flex>
            <v-flex md4>
              <v-pagination
                @input="onPageChange"
                :length="pagination.pageCount"
                circle
                total-visible="5"
                v-model="pagination.page"
              />
            </v-flex>
            <v-flex md2></v-flex>
          </v-flex>
        </v-layout>
      </v-flex>
    </v-layout>

    <v-dialog v-model="dialog" width="500">
      <v-card>
        <v-card-title class="headline grey lighten-2" primary-title>Privacy Policy</v-card-title>

        <v-card-text>
          
        </v-card-text>

        <v-divider></v-divider>

        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="primary" flat @click="dialog = false">确定</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

    <v-dialog v-model="modal" persistent max-width="290">
      <v-card>
        <v-card-title class="title">{{ modalTitle }}</v-card-title>
        <v-card-text class="title">{{ modalContent }}</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" flat @click="onModalConfirm">确定</v-btn>
          <v-btn color="grey darken-1" flat @click="modal = false">取消</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </div>
</template>


<script lang="ts">
// 使用类装饰器
import { Component, Vue } from "vue-property-decorator";
import { Admin } from "@/api";
import { TaskStatusKV, PageOptions, UserTypeKV } from "@/utils/constants";
import { forEach } from "lodash";
import { KV, Pagination } from "@/utils/type";
import { getTime } from "@/utils/commonUtils";
import { RequestPageVo } from '@/api/bean';
declare const $Vue: Vue;

@Component
export default class TraceTask extends Vue {
  private userType: Array<KV> = [];

  private pageOptions: any = [];
  
  private queryForm: any = {
    nickName: "",
    userType: "",
  };

  private roleTypes: any = {};

  private getTime(date: string) {
    return getTime(date);
  }

  private headers: Array<any> = [
    { text: "ID", align: "center", sortable: false, value: "id" },
    { text: "用户名称", align: "center", sortable: false, value: "taskId" },
    { text: "所属角色", align: "center", sortable: false, value: "platform" },
    { text: "创建时间", align: "center", sortable: false, value: "createTime" },
    { text: "更新时间", align: "center", sortable: false, value: "bonus" },
    { text: "操作", align: "center", sortable: false, value: "action" }
  ];

  private tableData: Array<any> = [];

  private loading: boolean = false;
  private dialog: boolean = false;
  private modal: boolean = false;
  private modalType: number = 1;
  private modalTitle: string = "注意!";
  private modalContent: string = "确认删除该用户吗?";
  private isQuery: boolean = false;
  private modalItem: any = {};

  private pagination: Pagination = {
    pageCount: 0,
    page: 1,
    limit: 10,
    offset: 0
  };

  protected mounted() {
    this.getUserList();
    this.initialize();
  }

  private initialize() {
    this.userType = UserTypeKV.slice();
    this.pageOptions = PageOptions.slice();
  }

  /**
   * 处理角色列表
   */
  private handleRoleList(data: any) {
    let map: any = {};
    data.forEach((item: any) => {
      map[item.groupId] = item.groupName
    });
    this.roleTypes = map;
  }


  private onQuery(from?: string) {
    let params: any = {};
    let valid = 0;
    forEach(this.queryForm, (item, key) => {
      item ? (params[key] = item) : valid++;
    });
    if (valid === 2) {
      return $Vue.$emit("snackbar", {
        message: "请至少填写一个查询条件"
      });
    }
    if (from === undefined) {
      this.pagination.offset = 0;
      this.pagination.page = 1;
    }
    params.limit = this.pagination.limit;
    params.offset = this.pagination.offset;
    this.getUserList(params);
    this.isQuery = true;
  }

  // 新增用户
  private onAddUser() {
    this.$router.push({
      name: "新增用户",
      params: {
        type: "add"
      }
    });
  }

  private onModify(item: any) {
    this.$router.push({
      name: "新增用户",
      params: {
        type: "modify",
        user: item
      }
    });
  }

  // 停止活动
  private onDelete(item: any) {
    this.modalItem = item;
    this.modal = true;
    this.modalContent = "确认删除该用户吗?";
    this.modalType = 1;
  }

  private onPageChange(page: number) {
    this.pagination.offset = (page - 1) * this.pagination.limit;
    if (!this.isQuery) {
      this.getUserList();
    } else {
      this.onQuery("wyvonj");
    }
  }

  private onPageOptionChange(value: any) {
    this.pagination.page = 1;
    this.pagination.offset = 0;
  }


  private onModalConfirm() {
    if (this.modalType === 1) {
      this.deleteUser();
    }
  }

  /**
   * 获取任务列表
   */
  private async getUserList(queryForm?: any) {
    let params = {
      offset: this.pagination.offset,
      limit: this.pagination.limit
    };
    if (queryForm) {
      params = queryForm;
    }
    const { data } = await Admin.getUserList.r(params);
    console.log("getUserList", data);
    if (data.result === 0) {
      let $data = data.data;
      this.tableData = $data.dataList;
      this.pagination.pageCount = $data.totalCount
        ? Math.ceil($data.totalCount / this.pagination.limit)
        : 1;
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }


  /**
   * 删除用户
   */
  private async deleteUser() {
    const params = {
      userId: this.modalItem.userId,
    };
    const { data } = await Admin.deleteUser.r(params);
    console.log("queryActivity", data);
    $Vue.$emit("snackbar", {
      message: data.message
    });
    if (data.result === 0) {
      //
    } else {

    }
  }

  /**
   * 获取角色列表
   */
  private async getRoleList() {
    let params: RequestPageVo = {
      offset: 0,
      limit: 1000,
    };
    const { data } = await Admin.getGroupList.r(params);
    console.log("getRoleList", data);
    if (data.result === 0) {
      let $data = data.data;
      this.handleRoleList(data.data.dataList);
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }
}
</script>

<style lang="less" scoped>
.tracetask-container {
  width: 100%;
  height: 100%;
}
.pagination-wrapper {
  background: white;
  border-top: 1px solid #d4d4d4;
}
</style>


